using ServiceStack.Support;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
namespace ServiceStack
{
	public static class ActionExecExtensions
	{
		[CompilerGenerated]
		[Serializable]
		private sealed class <>c
		{
			public static readonly ActionExecExtensions.<>c <>9 = new ActionExecExtensions.<>c();
			public static WaitCallback <>9__1_0;
			public static Converter<IAsyncResult, WaitHandle> <>9__5_0;
			internal void <ExecAsync>b__1_0(object x)
			{
				((ActionExecHandler)x).Execute();
			}
			internal WaitHandle <WaitAll>b__5_0(IAsyncResult x)
			{
				return x.AsyncWaitHandle;
			}
		}
		public static void ExecAllAndWait(this ICollection<Action> actions, TimeSpan timeout)
		{
			WaitHandle[] array = new WaitHandle[actions.Count];
			int num = 0;
			foreach (Action current in actions)
			{
				array[num++] = current.BeginInvoke(null, null).AsyncWaitHandle;
			}
			ActionExecExtensions.WaitAll(array, timeout);
		}
		public static List<WaitHandle> ExecAsync(this IEnumerable<Action> actions)
		{
			List<WaitHandle> list = new List<WaitHandle>();
			foreach (Action arg_24_0 in actions)
			{
				AutoResetEvent autoResetEvent = new AutoResetEvent(false);
				list.Add(autoResetEvent);
				ActionExecHandler state = new ActionExecHandler(arg_24_0, autoResetEvent);
				WaitCallback arg_4A_0;
				if ((arg_4A_0 = ActionExecExtensions.<>c.<>9__1_0) == null)
				{
					arg_4A_0 = (ActionExecExtensions.<>c.<>9__1_0 = new WaitCallback(ActionExecExtensions.<>c.<>9.<ExecAsync>b__1_0));
				}
				ThreadPool.QueueUserWorkItem(arg_4A_0, state);
			}
			return list;
		}
		public static bool WaitAll(this List<WaitHandle> waitHandles, int timeoutMs)
		{
			return ActionExecExtensions.WaitAll(waitHandles.ToArray(), timeoutMs);
		}
		public static bool WaitAll(this ICollection<WaitHandle> waitHandles, int timeoutMs)
		{
			return ActionExecExtensions.WaitAll(waitHandles.ToArray<WaitHandle>(), timeoutMs);
		}
		public static bool WaitAll(this ICollection<WaitHandle> waitHandles, TimeSpan timeout)
		{
			return ActionExecExtensions.WaitAll(waitHandles.ToArray<WaitHandle>(), (int)timeout.TotalMilliseconds);
		}
		public static bool WaitAll(this List<IAsyncResult> asyncResults, TimeSpan timeout)
		{
			Converter<IAsyncResult, WaitHandle> arg_20_1;
			if ((arg_20_1 = ActionExecExtensions.<>c.<>9__5_0) == null)
			{
				arg_20_1 = (ActionExecExtensions.<>c.<>9__5_0 = new Converter<IAsyncResult, WaitHandle>(ActionExecExtensions.<>c.<>9.<WaitAll>b__5_0));
			}
			return ActionExecExtensions.WaitAll(asyncResults.ConvertAll<WaitHandle>(arg_20_1).ToArray(), (int)timeout.TotalMilliseconds);
		}
		public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
		{
			return ActionExecExtensions.WaitAll(waitHandles, (int)timeout.TotalMilliseconds);
		}
		public static bool WaitAll(WaitHandle[] waitHandles, int timeOutMs)
		{
			if (waitHandles == null)
			{
				throw new ArgumentNullException("waitHandles");
			}
			if (waitHandles.Length == 0)
			{
				return true;
			}
			if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
			{
				bool flag = true;
				for (int i = 0; i < waitHandles.Length; i++)
				{
					WaitHandle waitHandle = waitHandles[i];
					flag = (flag && waitHandle.WaitOne(timeOutMs, false));
				}
				return flag;
			}
			return WaitHandle.WaitAll(waitHandles, timeOutMs, false);
		}
	}
}
